package com.boot.security.server.controller.mffl;

import java.util.List;
import java.util.Map;

import com.boot.security.server.utils.SmsUtils;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.SysSmsDao;
import com.boot.security.server.model.mffl.SysSms;
import com.boot.security.server.utils.mffl.NewSmsUtils;

import io.swagger.annotations.ApiOperation;

@Api(tags = "短信验证")
@RestController
@RequestMapping("/mffl")
public class SysSmsController {

    @Autowired
    private SysSmsDao sysSmsDao;

    @PostMapping("/sendSms")
    @ApiOperation(value = "短信验证码发送")
    public ReturnBean SendSms(@RequestParam String phone) throws Exception {
        ReturnBean returnBean = new ReturnBean();
        if (StringUtils.isEmpty(phone)) {
            returnBean.setErrCode("400");
            returnBean.setMessage("手机号不能为空！");
            return returnBean;
        }
        //创建短信发送工具
        NewSmsUtils newSmsUtils = new NewSmsUtils();
        String vcode = SmsUtils.vcode();
        Map<String, String> response = newSmsUtils.sendSms(sysSmsDao, phone, "您的验证码：" + vcode + "，您正进行身份验证，打死不告诉别人！", vcode);

        if (response.get("returnstatus").equals("Success") && response.get("message").equals("ok")) {
            return returnBean;
        } else {
            returnBean.setErrCode("105");
            returnBean.setMessage("操作失败请稍后再试！");
            return returnBean;
        }

    }


    /*@PostMapping("/sendSms")
    @ApiOperation(value = "短信验证码发送")
    public ReturnBean SendSms(@RequestParam String phone) {
        String response = SmsUtils.sendSms(sysSmsDao,phone,"","1",SmsUtils.vcode());
        String code = "200";
        if(response.replace("invalid mobile number","").length() < response.length()){
            code = "400";
            response = "请输入正确的手机号!";
        }
        return new ReturnBean(code,response);
    }*/


    /* 后台接口 */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public SysSms get(@PathVariable Long id) {
        return sysSmsDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public SysSms update(@RequestBody SysSms sysSms) {
        sysSmsDao.update(sysSms);

        return sysSms;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return sysSmsDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<SysSms> list(PageTableRequest request) {
                return sysSmsDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        sysSmsDao.delete(id);
    }
}
